const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    deviceInfo:{},
    switchPowerBool:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData();
    this.setData({ AliasName:options.AliasName });
    wx.setNavigationBarTitle({
      title: options.AliasName
    });
  },

  //获取上一页传递来的数据
  getData:function(){
    let that = this;
    const eventChannel = that.getOpenerEventChannel();
    eventChannel.on('getDeviceData',function(data){
      that.setData({
        deviceInfo:data.data.info
      });
      console.log(that.data.deviceInfo);
    })
  },
  
  //开关
  switchPower:function(){    
    let that = this;
    let switchPowerNum;
    let switchPowerBool = that.data.switchPowerBool;
    switchPowerBool = !switchPowerBool;
    if (switchPowerBool) {
      switchPowerNum = 1;
    }else{
      switchPowerNum = 0;
    }
    app.sdk.init().then(()=>{
      let data = { };
      data.DeviceId = that.data.deviceInfo.DeviceId;
      data.Data=`{\"power_switch\":${switchPowerNum}}`;
      app.sdk.requestApi('AppControlDeviceData',data)
       .then((res)=>{
         console.log(res);
       })
       .catch((err)=>{
         console.log(err);
       })
    });
    that.setData({
      switchPowerBool:switchPowerBool
    })
  },

  //跳转到设备详情页
  goToChooseMore:function(e){
    let that = this;
    wx.navigateTo({
      url: `/pages/deviceInfo/detailInfo/index?AliasName=${that.data.AliasName}`,
      success: (result) => {
         result.eventChannel.emit('getDeviceInfo',{ data:e.currentTarget.dataset });
      },
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})